var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: app.config,
    globalData: app.globalData,
    subProductIds: '',
    screenWidth: 0,
    screenHeight: 0,
    selectedRedbagIndex: -1,
    redbags: [],
    emptyPageData: {
      message: '没有任何红包',
      buttonText: '返回',
      buttonEvent: 'goBack'
    }
  },

  selectRedbag: function (e) {
    console.log(e.currentTarget.dataset.idx)
    this.setData({
      selectedRedbagIndex: e.currentTarget.dataset.idx
    });

    var redbagId = this.data.redbags[e.currentTarget.dataset.idx].id;

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      redbagId: redbagId,
      redbag: e.currentTarget.dataset.name,
      redbagAmount: this.data.redbags[e.currentTarget.dataset.idx].unit_price
    })

    // setTimeout(function () {
      wx.navigateBack({

      });
    // }, 1000)

  },

  goBack: function (e) {
    wx.navigateBack({
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.data.subProductIds = options.subProductIds ? options.subProductIds : null
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    this.setData({
      screenWidth: screenWidth,
      screenHeight: screenHeight,
      redbagId: options.redbagId
    });
  },

  onShow: function () {
    var that = this;
    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      iFDU.api.doGet(
        'redbag/getRealRedbagList',
        {
          userId: globalData.userId,
          subProductIds: that.data.subProductIds ? that.data.subProductIds : null
        },
        function (data) {
          that.setData({
            redbags: data
          });
        }
      )
    });


    
  }
})